/* Copyright Paul Reiche, Fred Ford. 1992-2002, Christopher Night 2013

This file is part of the HTML5 port of Ur-Quan Masters (UQM-HTML5)

UQM-HTML5 is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

UQM-HTML5 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with this program.  If not,
see <http://www.gnu.org/licenses/>. */


races.human = Object.create(races.base)

races.human.ship_info = {
	max_crew: 18,
	max_energy: 18,
	energy_regeneration: 1,
	weapon_energy_cost: 9,
	special_energy_cost: 4,
	energy_wait: 8,
	max_thrust: 24,
	thrust_increment: 3,
	turn_wait: 1,
	thrust_wait: 4,
	weapon_wait: 10,
	special_wait: 9,
	mass: 6,
	farrayname: "human/human.big",
}

races.human.nuke_info = {
	speed: 40,
	max_speed: 80,
	pixoffs: 42,
	hit_points: 1,
	damage: 4,
	life: 60,
	blast_offs: 8,
	track_wait: 3,
	// TODO: this is a hack I'm adding because I can't figure out why your point defenses don't
	//   kill nukes you just launched in the real game.
	flags: {
		IGNORE_SIMILAR: true,
	},
	preprocess_func: function () {
		if (this.turn_wait > 0) {
			--this.turn_wait
		} else {
			weapon.TrackShip.apply(this)
			this.next.frame = this.frames[this.facing]
			this.turn_wait = this.info.track_wait
		}
		var speed = Math.min(
			this.info.speed + (this.info.life - this.life_span) * 4,
			this.info.max_speed
		) * 0.25, A = this.facing * tau / 16
		this.vx = speed * Math.sin(A)
		this.vy = speed * Math.cos(A)
		
	},
}



races.human.init_weapon_func = function () {
	if (!races.human.nuke_info.frames) {
		races.human.nuke_info.frames = graphics.getfarray("human/saturn.big")
	}
	var nuke = new Missile(races.human.nuke_info, this)
	return [nuke]
}

// uqm/races/human.c:spawn_point_defense
races.human.init_special_func = function () {
	var targets = [], laserrange = 100
	// this loop runs backward but I don't see why it makes a difference.
	for (var j = 0 ; j < elements.length ; ++j) {
		var element = elements[j]
		if (element === this) continue
		if (element.state.SKIP_COLLISION) continue
		if (element.state.CLOAKED) continue
		// TODO: the reason I'm putting this here is because a weapon that was just spawend might
		// not have had its first preprocess and thus its next won't be defined. There might be a
		// better way to handle this.
		if (element.state.APPEARING) continue
		var dx = zmod(element.next.x - this.next.x, ARENA_WIDTH)
		var dy = zmod(element.next.y - this.next.y, ARENA_HEIGHT)
		if (dx * dx + dy * dy > laserrange * laserrange) continue
		targets.push(element)
	}
	if (!targets.length) return null
	for (var j = 0 ; j < targets.length ; ++j) {
		var defense = new Laser({
			pixoffs: 0,
			flags: {
				IGNORE_SIMILAR: true,
			},
			target: targets[j],
			color: "white",
		}, this)
		elements.push(defense)
	}
	return targets
}



